package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.ydkj.a37e_mall.Classes.Model.Request.GetProvinceReq;
import com.ydkj.a37e_mall.Classes.Model.Response.GetAddressCityResp;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_get;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.GsonImpl;
import com.ydkj.a37e_mall.Resource.Utils.Rsa;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by hong on 2016/3/10.
 * <p>
 * 选择城市
 */
public class ChooseAddressCity extends BaseActivity {

    private Activity mActivity;
    private ChooseCityAdapter chooseCityAdapter;
    private List<GetAddressCityResp.ResultEntity.DataEntity> dataEntityList;
    private ListView lv_address;
    private ImageView img_back;
    private String code;
    private String province_code;
    private String province_name;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_address);
        mActivity = this;
        init();
    }


    private void init() {

        if (this.getIntent().getExtras() != null) {
            code = this.getIntent().getExtras().getString("code");
            province_code = this.getIntent().getExtras().getString("province_code");
            province_name = this.getIntent().getExtras().getString("province_name");
            Log.e("ssss", "code==>" + code);
            Log.e("url", "province_name==" + province_name);
        }
        GetCity();

        img_back = (ImageView) mActivity.findViewById(R.id.img_back);
        lv_address = (ListView) mActivity.findViewById(R.id.lv_address);
        dataEntityList = new ArrayList<GetAddressCityResp.ResultEntity.DataEntity>();
        chooseCityAdapter = new ChooseCityAdapter(dataEntityList);
        lv_address.setAdapter(chooseCityAdapter);

        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private class ChooseCityAdapter extends BaseAdapter {
        private List<GetAddressCityResp.ResultEntity.DataEntity> dataEntityList;

        private ChooseCityAdapter(List<GetAddressCityResp.ResultEntity.DataEntity> infos) {
            if (infos == null) {
                infos = new ArrayList<GetAddressCityResp.ResultEntity.DataEntity>();
            }
            dataEntityList = infos;
        }

        @Override
        public int getCount() {
            return dataEntityList.size();
        }

        /**
         * 获得某一位置的数据
         */
        @Override
        public GetAddressCityResp.ResultEntity.DataEntity getItem(int position) {
            return dataEntityList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder vh = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.choose_address_listitem, null);
                vh = new ViewHolder();
                vh.tv_address = (TextView) convertView.findViewById(R.id.bankname);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            convertView.setTag(vh);
            convertView.setClickable(true);

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ChooseAddressCity.this, ChooseAddressArea.class);
                    intent.putExtra("code", getItem(position).getCode());
                    intent.putExtra("city_code", getItem(position).getId());
                    intent.putExtra("city_name", getItem(position).getName());
                    intent.putExtra("province_code", province_code);
                    intent.putExtra("province_name", province_name);
                    startActivity(intent);
                }
            });

            vh.tv_address.setText(getItem(position).getName());

            return convertView;
        }

        private class ViewHolder {
            public TextView tv_address;
        }
    }

    private void GetCity() {
        CustomProgressDialog.createDialog(mActivity).Show();
        GetProvinceReq req = new GetProvinceReq();
        req.setType("city");
        req.setParent(code + "");
        String url = Const.COMMON_ADDRESS + req.urlString();
        try {
            String source = req.getString();
            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
        } catch (Exception e) {
            e.printStackTrace();
        }

        params.put("secret", secret);
        Log.e("url", "choose_city_params==" + params);

        Async_http_get.get(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("url", "city_statusCode__onSuccess==" + statusCode);
                CustomProgressDialog.Dismiss();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                Log.e("url", "response==" + response);
                if (errno.equals("000")) {
                    GetAddressCityResp resp = GsonImpl.get().toObject(response.toString(), GetAddressCityResp.class);
                    //把数据填入Resp后，再把数据填入list里
                    dataEntityList.clear();
                    dataEntityList.addAll(resp.getResult().getData());
                    chooseCityAdapter.notifyDataSetChanged();
                }
                if (!errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Log.e("url", "city_statusCode==" + statusCode);
                Log.e("url", "responseString==" + responseString);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }
}
